<template>
  <div class="window" v-if="visible">
    <div class="window-mask" v-if="visible" @click="close"></div>

    <div class="close" @click="close">
      <span class="iconfont">×</span>
    </div>

    <div
      class="window-content"
      :style="{
        left: windowContentLeft + 'px',
        width: windowContentWidth + 'px'
      }"
    >
      <div class="title">
        {{ title }}
      </div>
      <div class="content-body">
        <slot></slot>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed, onMounted, onUnmounted, ref } from 'vue'

const props = defineProps({
  visible: {
    type: Boolean
  },
  width: {
    type: Number,
    default: 1000
  },
  title: {
    type: String
  },
  align: {
    type: String,
    default: 'top'
  }
})

const windowWidth = ref(window.innerWidth)
const windowContentWidth = computed(() => {
  return props.width > windowWidth.value ? windowWidth.value : props.width
})

const windowContentLeft = computed(() => {
  let left = windowWidth.value - props.width
  return left < 0 ? 0 : left / 2
})

const emit = defineEmits(['close'])
const close = () => {
  emit('close')
}

const resizeWindow = () => {
  windowWidth.value = window.innerWidth
}

onMounted(() => {
  window.addEventListener('resize', resizeWindow)
})

onUnmounted(() => {
  window.removeEventListener('resize', resizeWindow)
})
</script>

<style lang="scss" scoped>
$margin-top-bottom: 2vh;

.window {
  .window-mask {
    top: 0px;
    left: 0px;
    width: 100%;
    height: calc(100vh);
    z-index: 10000;
    opacity: 0.5;
    background: #000;
    position: fixed;
  }

  .close {
    z-index: 10002;
    cursor: pointer;
    position: absolute;
    top: 40px;
    right: 30px;
    width: 44px;
    height: 44px;
    border-radius: 22px;
    background: #606266;
    display: flex;
    justify-content: center;
    align-items: center;

    .iconfont {
      font-size: 20px;
      color: #fff;
      z-index: 200000;
    }
  }

  .window-content {
    top: $margin-top-bottom;
    border-radius: 5px;
    z-index: 10001;
    position: absolute;
    background: #fff;

    .title {
      text-align: center;
      line-height: 40px;
      border-bottom: 1px solid #ddd;
      font-weight: bold;
    }

    .content-body {
      height: calc(100vh - #{$margin-top-bottom * 2} - 40px);
      display: flex;
      overflow: auto;
    }
  }
}
</style>
